var path = require('path');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var vue = require("vue-loader");
var SpritesmithPlugin = require('webpack-spritesmith')
// 配置异步加载地址
var project_publicPath = process.env.NODE_ENV === 'production' ? __dirname + '/build/' : '/build/';

// var project_publicPath = "http://192.168.3.5:9090/res/build";

//定义了一些文件夹的路径
var ROOT_PATH = path.resolve(__dirname);
var APP_PATH = path.resolve(ROOT_PATH, 'src/main.js');

var BUILD_PATH = path.resolve(ROOT_PATH, 'build');
// var BUILD_PATH = path.resolve(ROOT_PATH, '../../../Program Files (x86)/tomecat-8.develop/apache-tomcat-8.0.35-windows-x64/apache-tomcat-8.0.35/webapps/steam/res/build')//"D:/Program Files (x86)/tomecat-8.develop/apache-tomcat-8.0.35-windows-x64/apache-tomcat-8.0.35/webapps/steam/res/build"//path.resolve(ROOT_PATH, 'build');

var plugins = [
  // new webpack.ProvidePlugin({
  //     $:"jquery",  
  //     jQuery:"jquery",  
  //     "windows.jQuery":"jquery"  
  // }),
  new SpritesmithPlugin({
      src: {
          cwd: path.resolve(__dirname, 'src/img/sprites'),
          glob: '*.png'
      },
      target: {
          image: path.resolve(__dirname, 'build/sprite/sprite.png'),
          css: path.resolve(__dirname, 'build/sprite/sprite.css')
      },
      apiOptions: {
          cssImageRef: "./sprite.png"
      },
      spritesmithOptions: {
        algorithm: 'top-down'
      }
  }),
	//压缩js 
	// new webpack.optimize.UglifyJsPlugin({minimize: true}),
	//提公用js到common.js文件中
  new webpack.optimize.CommonsChunkPlugin('common.js'),
	//将样式统一发布到style.css中
  new ExtractTextPlugin("style.css", {
    allChunks: true
  }),
  // 使用 ProvidePlugin 加载使用率高的依赖库
  new webpack.ProvidePlugin({
    $: 'webpack-zepto'
  })
];

module.exports = {
  //项目的文件夹 可以直接用文件夹名称 默认会找index.js 也可以确定是哪个文件名字
  entry: [
    APP_PATH
  ],
  //输出的文件名 合并以后的js会命名为bundle.js
  output: {
    path: BUILD_PATH,
    filename: 'build.js',
    // 指向异步加载的路径
    // 博客异步加载地址
    // publicPath :  '/mystatic/CNode_Vue_Spa' + '/build/',
    // 异步加载地址
    publicPath : "http://192.168.4.101:8089/build/",//project_publicPath,
    // 非主文件的命名规则
    chunkFilename: '[id].build.js'//?[chunkhash]
  },
  module: {
    loaders: [
      {
        test: /\.vue$/,
        loader: 'vue',
      },
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract("style-loader", 'css-loader')
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract("style-loader", "css-loader")
      },
      {
        test: /\.(png|jpg|gif)$/,
        loader: 'url?limit=40000'
      },
      {
       test   : /\.woff|\.woff2|\.svg|.eot|\.ttf/,
       loader : 'url?prefix=font/&limit=10000'
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel'
      }
    ]
  },
  vue: {
    css: ExtractTextPlugin.extract("css"),
    sass: ExtractTextPlugin.extract("css!sass-loader")
  },
  babel: {
    // 告诉babel你要解析的语言
    presets: ['es2015']
  },
  // devServer: {
  //   historyApiFallback: true,
  //   hot: true,
  //   inline: true,
  //   progress: true,
  // },
  // 
  devServer: {
    publicPath: "http://192.168.4.101:8089/build/",//project_publicPath,
    hot:true,
    inline: true,
    progress: true,
    historyApiFallback: true,
    host:"192.168.4.101",
    port: 8089,
    stats: { colors: true },
    proxy: {
      '/api/*': {
          target: 'http://192.168.4.101:9090',
          secure: false,
          changeOrigin: true,
          pathRewrite: {'/api': ''}
      }
    }
  },
  plugins: plugins,
  devtool: '#source-map'
};
